import {createRouter, createWebHistory} from 'vue-router'
import {defineAsyncComponent} from 'vue'

const router = createRouter({
  // history: createWebHashHistory(),  // hash 模式
  history: createWebHistory(),  // history 模式
  routes: [
    {
      path: '/list/:brand',
      name: 'list',
      component: defineAsyncComponent(() => import(`../views/list.vue`)),
      meta: {
        title: '列表页',
      },
    },
    {
      path: '/',
      name: 'home',
      component: defineAsyncComponent(() => import(`../views/home.vue`)),
      meta: {
        title: '首页',
      },
    },
    {
      path: '/detail/:wareId',
      name: 'detailPage',
      component: defineAsyncComponent(() => import(`../views/detailPage.vue`)),
      meta: {
        title: '详情页',
      },
    },
    {
      path: '/printBrandPage/:menuItem',
      name: 'printBrandPage',
      component: defineAsyncComponent(
          () => import(`../views/printBrandPage.vue`)),
      meta: {
        title: '印刷机品牌页',
      },
    }, {
      path: '/partBrandPage/:menuItem',
      name: 'partBrandPage',
      component: defineAsyncComponent(
          () => import(`../views/partBrandPage.vue`)),
      meta: {
        title: '配件品牌页',
      },
    }, {
      path: '/newBrandPage/:menuItem',
      name: 'newBrandPage',
      component: defineAsyncComponent(
          () => import(`../views/newBrandPage.vue`)),
      meta: {
        title: '新产品展示',
      },
    }, {
      path: '/afterBrandPage/:menuItem',
      name: 'afterBrandPage',
      component: defineAsyncComponent(
          () => import(`../views/afterBrandPage.vue`)),
      meta: {
        title: '印后设备',
      },
    }, {
      path: '/customBrandPage/:menuItem',
      name: 'customBrandPage',
      component: defineAsyncComponent(
          () => import(`../views/customBrandPage.vue`)),
      meta: {
        title: '按需定制',
      },
    }, {
      path: '/disCountBrandPage/:menuItem',
      name: 'disCountBrandPage',
      component: defineAsyncComponent(
          () => import(`../views/disCountBrandPage.vue`)),
      meta: {
        title: '促销专区',
      },
    }, {
      path: '/searchResult/:keyWord',
      name: 'searchResult',
      component: defineAsyncComponent(
          () => import(`../views/searchResult.vue`)),
      meta: {
        title: '搜索结果',
      },
    },{
      path: '/company',
      name: 'company',
      component: defineAsyncComponent(
          () => import(`../views/companyPage.vue`)),
      meta: {
        title: '公司简介',
      },
    },{
      path: '/activity',
      name: 'activityPage',
      component: defineAsyncComponent(
          () => import(`../views/activityPage.vue`)),
      meta: {
        title: '公司活动',
      },
    },{
      path: '/contact',
      name: 'contact',
      component: defineAsyncComponent(
          () => import(`../views/contactPage.vue`)),
      meta: {
        title: '联系我们',
      },
    },
    {
      path: '/*',
      redirect: '/',
    },
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to, from)
  if (to.meta.title) {
    document.title = `${to.meta.title}`;
  }
  next()
})

router.afterEach((to, from) => {
  console.log("to:{}, from:{}", to, from)
})

export default router